package com.wjk.kylin.mall.product.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import com.wjk.kylin.mall.common.redis.utils.RedisUtil;
import com.wjk.kylin.mall.product.bo.ProductBO;
import com.wjk.kylin.mall.product.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    @SentinelResource(value = "demo",
            blockHandler = "blockHandler",blockHandlerClass = TestServiceBlackHandlerImpl.class,
            fallback = "fallback",fallbackClass = TestServiceFallbackImpl.class
            )
    public BaseOutDTO demo() {
        DataOutDTO<String> dto = new DataOutDTO<>();
        dto.setData("TestService.demo");
        return dto;
    }

    @Override
    public BaseOutDTO setRedis(String val) {
        redisUtil.set("redis_demo",val);
        List<ProductBO> l = new ArrayList<>();
        ProductBO p = new ProductBO();
        p.setCreateTime(new Date());
        p.setId(110L);
        l.add(p);
        redisUtil.set("redis_demo_list",l);
        return DataOutDTO.success();
    }

    @Override
    public BaseOutDTO getRedis() {
        Object redisVal = redisUtil.get("redis_demo");
        Object lo = redisUtil.get("redis_demo_list");
        DataOutDTO<Object> outDTO = new DataOutDTO<>();
        outDTO.setData(lo);
        return outDTO;
    }

}
